package com.zx._03_并发.chapter_05_concurrent;

import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StopWatch;

/**
 * CopyOnWriteArrayList
 */
@Slf4j
public class Demo_01 {

    public static void main(String[] args) {
        StopWatch sw = new StopWatch();//计时器
        int total = 5000;
        String context = "123";
        CopyOnWriteArrayList<String> a = new CopyOnWriteArrayList<>();
        ArrayList<String> b = new ArrayList<>();

        sw.start("ArrayList");
        for (int i = 0; i < total; i++) {
            b.add(context);
        }
        sw.stop();

        sw.start("CopyOnWriteArrayList");
        for (int i = 0; i < total; i++) {
            a.add(context);
        }
        sw.stop();

        log.info(sw.prettyPrint());
    }
}
